package com.spring.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import com.spring.service.UserService;
import com.spring.service.UserServiceImpl;

//cglib代理
public class UserServiceProxyFactory2 implements MethodInterceptor{
	
	public UserService getUserServiceProxy() {
		Enhancer en = new Enhancer();//帮我们生成代理对象
		en.setSuperclass(UserServiceImpl.class);//设置对谁代理
		en.setCallback(this);//要做什么事
		UserService us = (UserService)en.create();
		return us;
	}

	@Override
	public Object intercept(Object arg0, Method method, Object[] arg2, MethodProxy methodProxy) throws Throwable {
		System.out.println("打开事务");
		Object obj = methodProxy.invokeSuper(arg0, arg2);
		System.out.println("提交事务");
		return obj;
	}


	
}
